<template>
  <div class="index-lists">
    <el-card class="!border-none" shadow="never">
      <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
        <el-form-item label="操作人姓名" prop="operatorName">
          <el-input class="w-[180px]" v-model="queryParams.operatorName"/>
        </el-form-item>
        <el-form-item label="操作设备地址" prop="operatingEquipment">
          <el-input class="w-[180px]" v-model="queryParams.operatingEquipment"/>
        </el-form-item>
        <el-form-item label="操作时间" prop="creationTime">
          <daterange-picker
              v-model:startTime="queryParams.creationTimeStart"
              v-model:endTime="queryParams.creationTimeEnd"
          />
        </el-form-item>
        <el-form-item label="操作端" prop="status">
          <el-select
              v-model="queryParams.operatingTerminal"
              class="w-[180px]"
              clearable
          >
            <el-option label="安卓端" value="1"/>
            <el-option label="PC端" value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none mt-4" shadow="never">
      <!--            <div>-->
      <!--                <el-button v-perms="['logs:add']" type="primary" @click="handleAdd()">-->
      <!--                    <template #icon>-->
      <!--                        <icon name="el-icon-Plus" />-->
      <!--                    </template>-->
      <!--                    新增-->
      <!--                </el-button>-->
      <!--            </div>-->
      <el-table
          class="mt-4"
          size="large"
          v-loading="pager.loading"
          :data="pager.lists"
      >
        <el-table-column label="操作人姓名" prop="operatorName" min-width="100"/>
        <el-table-column label="设备名称" prop="deviceName" min-width="100"/>
        <el-table-column label="操作变量" prop="operationalVariable" min-width="100"/>
        <el-table-column label="操作设备地址" prop="operatingEquipment" min-width="100"/>
        <el-table-column label="操作时间" prop="creationTime" min-width="100"/>
        <el-table-column label="来源ip" prop="sourceIp" min-width="100"/>
        <el-table-column label="操作端" prop="operatingTerminal" min-width="100">
          <template #default="{row}">
            <span v-if="row.operatingTerminal == 1">安卓端</span>
            <span v-else>PC端</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template #default="{ row }">
            <el-button v-perms="['device:edit']" type="primary"
                       link @click="handleEdit(row)">
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="flex justify-end mt-4">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup
        v-if="showEdit"
        ref="editRef"
        @success="getLists"
        @close="showEdit = false"
    />
  </div>
</template>
<script lang="ts" setup name="logs">
import {logsDelete, logsLists} from '@/api/logs'
import {usePaging} from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
  operatorId: '',
  operatorName: '',
  operationalVariable: '',
  operatingEquipment: '',
  operatingTime: '',
  creationTimeStart: '',
  creationTimeEnd: '',
  operatingTerminal: ''
})

const {pager, getLists, resetPage, resetParams} = usePaging({
  fetchFun: logsLists,
  params: queryParams
})


const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
  await feedback.confirm('确定要删除？')
  await logsDelete({id})
  feedback.msgSuccess('删除成功')
  getLists()
}

getLists()
</script>
